<?php

namespace app\controller;


use app\model\FactoryMaterial;
use app\model\UserFactory;
use app\model\UserIngredient;
use app\service\UserFactoryService;
use support\Log;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

//用户工厂控制器
class UserFactoryController extends Base
{



    public function get_factory_status(): Response
    {
        try {
            $res = (new UserFactoryService())->get_factory_status($this->user_id);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }
        return $this->json($res);

    }


    //生产队列状态
    public function get_factory_queue_status(): Response
    {
        try {
            $res = (new UserFactoryService())->get_factory_queue_status($this->user_id,0);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }
        return $this->json($res);

    }



    /**
     * 工厂列表
     */
    public function list(): Response
    {
        try {
            $res = (new UserFactoryService())->list($this->user_id);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }
        return $this->json($res);
    }


    /**
     * 工厂详情
     */
    public function index(): Response
    {

        $user_factory_id = $this->request->get('user_factory_id');
        try {
            $res = (new UserFactoryService())->details($this->user_id, $user_factory_id);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }
        return $this->json($res);


    }


    /**
     * 购买工厂
     */
    public function buy(): Response
    {
        //获取工厂ID
        $factory_id = $this->request->post('factory_id');
        Db::startTrans();
        try {
            $res = (new UserFactoryService())->buy_factory($this->user_id, $factory_id);
            if ($res['code']) {
                Db::commit();
            } else {
                Db::rollback();
            }
            return $this->json($res);

        } catch (\Exception $e) {
            Db::rollback();
            return $this->fail($e->getMessage());
        }


    }


    /**
     * 建造工厂
     */
    public function building(): Response
    {
        //获取工厂ID
        $user_factory_id = $this->request->post('user_factory_id');
        $position = $this->request->post('position',null);
        Db::startTrans();
        try {
            $res = (new UserFactoryService())->building($this->user_id, $user_factory_id, FACTORY_BUILD_STATUS['建造'],$position);
            if ($res['code']) {
                Db::commit();
            } else {
                Db::rollback();
            }
            return $this->json($res);

        } catch (\Exception $e) {
            Db::rollback();
            return $this->fail($e->getMessage());
        }
    }


    /**
     * 工厂升级
     */
    public function upgrade(): Response
    {
        //获取工厂ID
        $user_factory_id = $this->request->post('user_factory_id');
        Db::startTrans();
        try {
            $res = (new UserFactoryService())->building($this->user_id, $user_factory_id, FACTORY_BUILD_STATUS['升级']);
            if ($res['code']) {
                Db::commit();
            } else {
                Db::rollback();
            }
            return $this->json($res);

        } catch (\Exception $e) {
            Db::rollback();
            return $this->fail($e->getLine());
        }
    }


    //建造完成-收取材料
    public function complete(): Response
    {

        //获取工厂ID
        $user_factory_id = $this->request->post('user_factory_id');
        Db::startTrans();
        try {
            $res = (new UserFactoryService())->complete($this->user_id, $user_factory_id);
            if ($res['code']) {
                Db::commit();
            } else {
                Db::rollback();
            }
            return $this->json($res);

        } catch (\Exception $e) {
            Db::rollback();
            return $this->fail($e->getMessage());
        }


    }


    //生产
    public function production(): Response
    {
        //获取工厂ID
        $user_factory_id = $this->request->post('user_factory_id');
        $material_id = $this->request->post('material_id');


        Db::startTrans();
        try {
            $res = (new UserFactoryService())->production($this->user_id, $user_factory_id, $material_id);
            if ($res['code']) {
                Db::commit();
            } else {
                Db::rollback();
            }
            return $this->json($res);

        } catch (\Exception $e) {
            Db::rollback();
            return $this->fail($e->getMessage());
        }


    }


    //获取工厂工具
    public function pick(): Response
    {
        //获取工厂ID
        $user_factory_id = $this->request->post('user_factory_id');
        $seat_id = $this->request->post('seat_id');//槽位ID


        Db::startTrans();
        try {
            $res = (new UserFactoryService())->pick($this->user_id, $user_factory_id);
            if ($res['code']) {
                Db::commit();
            } else {
                Db::rollback();
            }
            return $this->json($res);

        } catch (\Exception $e) {
            Db::rollback();
            return $this->fail($e->getMessage());
        }


    }

    //改变工厂位置
    public function change_position(): Response
    {
        //获取工厂ID
        $user_factory_id = $this->request->post('user_factory_id',0);
        $position = $this->request->post('position',null);

        Db::startTrans();
        try {
            $res = (new UserFactoryService())->change_position($this->user_id, $user_factory_id, $position);
            if ($res['code']) {
                Db::commit();
            } else {
                Db::rollback();
            }
            return $this->json($res);

        } catch (\Exception $e) {
            Db::rollback();
            return $this->fail($e->getMessage());
        }
    }




}